package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.constant.CacheConstants;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.AlarmRulesDTO;
import com.iwomy.secureplat.platform.entity.AlarmDetectionTypeEntity;
import com.iwomy.secureplat.platform.entity.AlarmRulesEntity;
import com.iwomy.secureplat.platform.service.AlarmRulesService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 告警规则
 *
 * @author pig
 * @date 2023-12-22 10:09:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmRules")
@Tag(description = "alarmRules", name = "告警规则管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmRulesController {

	private final AlarmRulesService alarmRulesService;

	/**
	 * 分页查询
	 *
	 * @param page       分页对象
	 * @param alarmRules 告警规则
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_view')")
	public R getAlarmRulesPage(@ParameterObject Page page, @ParameterObject AlarmRulesEntity alarmRules) {
		return R.ok(alarmRulesService.pageByScope(page, alarmRules));
	}

	/**
	 * 列表查询
	 *
	 * @param alarmRules 告警规则
	 * @return
	 */
	@Operation(summary = "列表查询", description = "列表查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_view')")
	public R getAlarmRulesList( @ParameterObject AlarmRulesEntity alarmRules) {
		return R.ok(alarmRulesService.listByScope( alarmRules));
	}


	/**
	 * 通过id查询告警规则
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_view')")
	public R getAlarmRulesById(@PathVariable("id") Long id) {
		return R.ok(alarmRulesService.getAlarmRulesById(id));
	}

	/**
	 * 新增告警规则
	 *
	 * @param alarmRulesDTO 告警规则
	 * @return R
	 */
	@Operation(summary = "新增告警规则", description = "新增告警规则")
	@SysLog("新增告警规则")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_add')")
	@CacheEvict(value = CacheConstants.EQUIPMENT_ALARM_RULES,allEntries = true)
	public R save(@RequestBody AlarmRulesDTO alarmRulesDTO) {
		alarmRulesDTO.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(alarmRulesService.savaAlarmRules(alarmRulesDTO));
	}

	/**
	 * 修改告警规则
	 *
	 * @param alarmRulesDTO 告警规则
	 * @return R
	 */
	@Operation(summary = "修改告警规则", description = "修改告警规则")
	@SysLog("修改告警规则")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_edit')")
	@CacheEvict(value = CacheConstants.EQUIPMENT_ALARM_RULES,allEntries = true)
	public R updateById(@RequestBody AlarmRulesDTO alarmRulesDTO) {
		return R.ok(alarmRulesService.updateAlarmRulesById(alarmRulesDTO));
	}

	/**
	 * 通过id删除告警规则
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除告警规则", description = "通过id删除告警规则")
	@SysLog("通过id删除告警规则")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_del')")
	@CacheEvict(value = CacheConstants.EQUIPMENT_ALARM_RULES,allEntries = true)
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmRulesService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmRules 查询条件
	 * @param ids        导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_alarmRules_export')")
	public List<AlarmRulesEntity> export(AlarmRulesEntity alarmRules, Long[] ids) {
		return alarmRulesService.list(Wrappers.lambdaQuery(alarmRules).in(ArrayUtil.isNotEmpty(ids), AlarmRulesEntity::getId, ids));
	}

	/**
	 * 启用禁用
	 *
	 * @param alarmRulesEntity
	 * @return R
	 */
	@Operation(summary = "启用禁用告警规则", description = "启用禁用告警规则")
	@SysLog(expression = "'启用禁用告警规则'.concat(#alarmRulesEntity.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody AlarmRulesEntity alarmRulesEntity) {
		return R.ok(alarmRulesService.updateById(alarmRulesEntity));
	}
}